package com.vrsys.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vrsys.domain.Comments;
import com.vrsys.domain.Product;
import com.vrsys.domain.session.Cart;
import com.vrsys.service.CommentsService;
import com.vrsys.service.ProductService;
import com.vrsys.util.Constants;
import com.vrsys.view.PageQuery;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private Cart cart;

    /*
     * This is Ajax calling.
     * @RequestBody cannot be used here since the MediaType is
     */
    @RequestMapping(value = "/moreproducts.od",method = RequestMethod.POST, consumes=MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody PageQuery<Product> queryMoreProducts(PageQuery<Product> pageQuery, @ModelAttribute("lang") String lang) {
        pageQuery.setLanguage(lang);
        try {
            productService.pagingProducts(pageQuery);
        } catch (Exception e) {
            logger.error("",e);
            pageQuery.setData(new ArrayList<Product>(0));
        }
        return pageQuery;
    }

    /*
     * This is Ajax calling.
     * 
     */
    @RequestMapping(value = "/{pid}/productdetails.od",method = RequestMethod.GET)
    public ModelAndView showProductDetails(@PathVariable("pid") String pid, ModelAndView mv) {
        try {
            Product product = productService.queryDetailsBytId(Integer.valueOf(pid).intValue());
            mv.addObject("product", productService.queryDetailsBytId(Integer.valueOf(pid).intValue()));

            // query comments
            PageQuery<Comments> pageQuery = new PageQuery<Comments>();
            commentsService.pagingComments(pageQuery, product.getProductId());
            mv.addObject("pageQuery", pageQuery);
            mv.addObject("cartSize",cart.getCartSize());

            mv.setViewName("productdetails");
        } catch (Exception e) {
            logger.error("",e);
            mv.setViewName("commonerror");
        }
        return mv;
    }

    /*
     * This is Ajax calling.
     * 
     */
    @RequestMapping(value = "/{pid}/addtocart.od",method = RequestMethod.POST)
    public @ResponseBody String[] addToCart(@PathVariable("pid") String pid) {
        String[] values = new String[2];
        byte result = 0x2;
        try {
            result = cart.putToCart(productService.queryDetailsBytId(Integer.valueOf(pid).intValue()));
        } catch (Exception e) {
            logger.error("",e);
        }
        values[0] = String.valueOf(cart.getProducts().size());
        if(0x0 == result)
            values[1] = Constants.CART_ADDING_SUCCEED;
        else if(0x1 == result)
            values[1] = Constants.CART_ADDING_DUPLICATED;
        else
            values[1] = Constants.CART_ADDING_FAILURE;
        return values;
    }
/*
    @RequestMapping(value = "/moreproducts1.od",method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody PageQuery<Product> queryMoreProducts1(@RequestBody PageQuery<Product> pageQuery) {
        List<Product> products;
        pageQuery.setLanguage(this.lang);
        try {
            products = productService.getProducts(pageQuery);
        } catch (Exception e) {
            logger.error("",e);
            pageQuery.setData(new ArrayList<Product>(0));
            return pageQuery;
        }
        pageQuery.setFromIndex(pageQuery.getFromIndex() + pageQuery.getToIndex());
        pageQuery.setData(null == products ? new ArrayList<Product>(0) : products);
        return pageQuery;
    }*/

    @RequestMapping(value = "/products.od",method = RequestMethod.GET)
    public String queryProducts(PageQuery<Product> pageQuery, Model model, @ModelAttribute("lang") String lang) {
        try {
            pageQuery.setFromIndex(0);
            pageQuery.setLanguage(lang);
            productService.pagingProducts(pageQuery);
        } catch (Exception e) {
            logger.error("{}",e);
            pageQuery.setData(new ArrayList<Product>(0));
        }
        model.addAttribute("pageQuery",pageQuery);
        model.addAttribute("cartSize",cart.getCartSize());
        return "productoutlet";
    }

    @RequestMapping(value = "/{pid}/buyproduct.od", method = RequestMethod.GET)
    public String buyProduct(@PathVariable("pid") int pId, ModelAndView mv) {
        if (!cart.checkProduct(pId))
            cart.putToCart(productService.queryDetailsBytId(pId));
        return "redirect:/cart/cartlist.od";
    }
}
